package com.itzyf.pokemondata.activity

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.avos.avoscloud.AVException
import com.avos.avoscloud.AVObject
import com.avos.avoscloud.AVQuery
import com.avos.avoscloud.FindCallback
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.activity.gallery.WebViewActivity
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.itzyf.pokemondata.utils.showErrToast
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 版本更新历史
 * @author 依风听雨
 * @version 创建时间：2019/01/30 15:58
 */
class HistoryActivity : BaseActivity() {
    var mDatas: MutableList<AVObject?> = ArrayList()
    private lateinit var mAdapter: QuickAdapter
    var index = 0


    override fun handlerActivity() {
        llSearch.visibility = View.GONE

        mAdapter = QuickAdapter(R.layout.item_history, mDatas)
        rv_list.adapter = mAdapter

        rv_list.layoutManager = LinearLayoutManager(this)
        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })

        mAdapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(this@HistoryActivity, WebViewActivity::class.java)
            intent.putExtra("httpUrl", mDatas[position]!!.getString("url"))
            intent.putExtra("title", mDatas[position]!!.getString("title"))
            startActivity(intent)
        }
        showProgressDialog()
        queryDatas()

    }

    fun queryDatas(index: Int = 0, pageSize: Int = 20) {
        val query = AVQuery<AVObject>("history")
        query.limit(pageSize)
        query.skip(index * pageSize)
        query.orderByDescending("date")
        query.findInBackground(object : FindCallback<AVObject>() {
            override fun done(avObjects: MutableList<AVObject>?, avException: AVException?) {
                dismissProgressDialog()
                if (avException != null) {
                    showErrToast(avException)
                    return
                }
                if (avObjects == null) {
                    return
                }
                if (index == 0) {
                    mDatas.clear()
                    pull_to_refresh.finishRefresh()
                }
                mDatas.addAll(avObjects)
                if (avObjects.size < pageSize) {
                    mAdapter.loadMoreEnd()
                } else {
                    mAdapter.loadMoreComplete()
                }
                mAdapter.notifyDataSetChanged()
            }

        })

    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<AVObject?>) :
        BaseQuickAdapter<AVObject, BaseViewHolder>(layoutResId, data) {
        private val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE)
        override fun convert(helper: BaseViewHolder, item: AVObject?) {
            helper.setText(R.id.text, item?.getString("title"))
            helper.setText(R.id.tvDate, sdf.format(item?.getDate("date")))
        }
    }


    override fun getContentViewId() = R.layout.activity_list
    override fun getToolbarTitle() = "活动及版本更新"

}